package org.ym.database.hibernate.query;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.ym.tools.StringTool;

import javax.persistence.criteria.JoinType;

/**
 * 关联表
 * @author ym
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class JoinKey {
    public static final String LINE = "_";
    /**
     * join的key值
     */
    private String key;
    /**
     * join的类型
     */
    private JoinType joinType;

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        JoinKey otherJoinKey = (JoinKey) o;

        return new EqualsBuilder().append(key, otherJoinKey.key).append(joinType, otherJoinKey.joinType).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37).append(key).append(joinType).toHashCode();
    }

    /**
     * 判断joinKey是否有效
     * @return
     */
    public boolean isEffective(){
        return StringUtils.isBlank(key) || joinType == null;
    }

    /**
     * 获取构造hashmap的key
     * @return 显示的key
     */
    public String getDisplayKey(){
        return StringTool.concatStr(key, LINE,joinType.toString());
    }
}
